package com.spic.business.ioc.controller;

import com.google.common.collect.Lists;
import com.spic.business.ioc.domain.ForecastPreModel;
import com.spic.business.ioc.domain.ForecastWindModel;
import com.spic.business.ioc.service.*;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * IocController
 *
 * @author xuxiaochang@chinasofi.com
 * @date 2022/4/8
 */
@RestController
public class IocController extends BaseController
{
    @Autowired
    ForecastService forecastService;

    @Autowired
    private IIocComplianceService iocComplianceService; //PM10达标情况
    @Autowired
    private IIocDustConcentrationService iocDustConcentrationService; //粉尘浓度数据
    @Autowired
    private IIocGoodDaysService iocGoodDaysService;  //优良天数
    @Autowired
    private IIocHazardousWasteTreatmentService iocHazardousWasteTreatmentService; //危废物处置情况数据
    @Autowired
    private IIocNoiseDataService iocNoiseDataService; //噪声数据
    @Autowired
    private IIocPmTwoPointFiveService iocPmTwoPointFiveService; //PM2.5达标情况
    @Autowired
    private IIocRainfallService iocRainfallService; //年合计降雨量
    @Autowired
    private IIocWasteDumpGreeningRateService iocWasteDumpGreeningRateService; //排土场治绿率数据

    @PostMapping("7dayForecast")
    public void get7dayForecastData(){
        String url = "https://m.weathercn.com/daily-weather-forecast.do?partner=1000001071_hfaw&id=1849236&p_source=&p_type=jump&day=";
        try {
            forecastService.deletePre();
            forecastService.deleteWind();
            LocalDate localDate = LocalDate.now();
            String regex = "\\d+";
            Pattern pattern = Pattern.compile(regex);
            for (int i = 1; i <= 7; i++) {
                Document doc = Jsoup.connect(url + i).timeout(5000)
                        .userAgent("PostmanRuntime/7.28.1").get();
                String wind = doc.select(".details-light > .days-details-right > p.js-wind").text();
                Matcher matcher = pattern.matcher(wind);
                if (matcher.find())
                {
                    wind = matcher.group();
                }
                String pre = doc.select(".rain-light > .days-rain-charts  .js-light-rain-mm").text();
                pre = pre.replace("mm", "");
                LocalDate tomo = localDate.plusDays(i - 1);
                String toStr = tomo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                ForecastWindModel windModel = new ForecastWindModel(toStr, Double.valueOf(wind));
                ForecastPreModel preModel = new ForecastPreModel(toStr, Double.valueOf(pre));
                forecastService.savePre(preModel);
                forecastService.saveWind(windModel);
            }


        } catch (IOException e) {
                
        }
    }




    /**
     * @return 获取最近6年集合列表
     */
    @GetMapping("get6Year")
    public AjaxResult getYear(){
        return AjaxResult.success(iocGoodDaysService.getYear());
    }
    /**
     * @return 优良天数数据
     */
    @GetMapping("/ioc/getGoodDays")
    public AjaxResult getGoodDays()
    {
       List<Map<String,Object>> goodDayListMap =  iocGoodDaysService.getGoodDays();
       return AjaxResult.success(goodDayListMap);
    }
    /**
     * @return PM10达标情况
     */
    @GetMapping("/ioc/getPm10Compliance")
    public AjaxResult getPm10Compliance()
    {
        List<Map<String,Object>> complianceListMap =  iocComplianceService.getPm10Compliance();
        return AjaxResult.success(complianceListMap);
    }

    /**
     * @return 年合计降雨量
     */
    @GetMapping("/ioc/getyearRainfall")
    public AjaxResult getyearRainfall()
    {
        List<Map<String,Object>> yearRainfallListMap =  iocRainfallService.getyearRainfall();
        return AjaxResult.success(yearRainfallListMap);
    }

    /**
     * @return PM2.5达标情况
     */
    @GetMapping("/ioc/getPm25")
    public AjaxResult getPm25()
    {
        List<Map<String,Object>> pmm25ListMap =  iocPmTwoPointFiveService.getPm25();
        return AjaxResult.success(pmm25ListMap);
    }

    /**
     * @return 噪声数据
     */
    @GetMapping("/ioc/getNoiseData")
    public AjaxResult getNoiseData()
    {
        List<Map<String,Object>> noiseDataListMap =  iocNoiseDataService.getNoiseData();
        return AjaxResult.success(noiseDataListMap);
    }

    /**
     * @return 排土场治绿率数据
     */
    @GetMapping("/ioc/getWasteDumpGreeningRate")
    public AjaxResult getWasteDumpGreeningRate()
    {
        List<Map<String,Object>> wasteDumpGreeningRateListMap =  iocWasteDumpGreeningRateService.getWasteDumpGreeningRate();
        return AjaxResult.success(wasteDumpGreeningRateListMap);
    }

    /**
     * @return 粉尘浓度数据
     */
    @GetMapping("/ioc/getDustConcentration")
    public AjaxResult getDustConcentration()
    {
        List<Map<String,Object>> dustConcentrationListMap =  iocDustConcentrationService.getDustConcentration();
        return AjaxResult.success(dustConcentrationListMap);
    }

    /**
     * @return 危废物处置情况数据
     */
    @GetMapping("/ioc/getHazardousWasteTreatment")
    public AjaxResult getHazardousWasteTreatment()
    {
        List<Map<String,Object>> hazardousWasteTreatmentListMap =  iocHazardousWasteTreatmentService.getHazardousWasteTreatment();
        return AjaxResult.success(hazardousWasteTreatmentListMap);
    }

    @GetMapping("demo")
    public AjaxResult demo(){
        double lon = 119.58957740989078;
        double lat = 45.498796;
        Map<String, Double> map = new HashMap<>();
        map.put("lon", lon);
        map.put("lat", lat);
        List<Map<String, Double>> list = Lists.newArrayList(map);
        return AjaxResult.success(list);
    }
}
